package com.example.day03_32.base;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public abstract class BaseRlvAdapter<T> extends RecyclerView.Adapter<BaseRlvAdapter.BaseViewHolder> {
    private Context context;
    public List <T> mList;
     private OnItemClickListener mOnItemTouchListener;
    public BaseRlvAdapter(Context context, List<T> mList) {
        this.context = context;
        this.mList = mList;
    }
    @NonNull
    @Override
    public BaseRlvAdapter.BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(getLayout(), parent, false);
        return new BaseViewHolder(inflate);
    }


    @Override
    public void onBindViewHolder(@NonNull BaseRlvAdapter.BaseViewHolder holder, final int position) {
            T t=mList.get(position);
            bidData(holder,t);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mOnItemTouchListener!=null){
                        mOnItemTouchListener.onItemClick(v,position);
                    }
                }
            });
    }

    protected abstract void bidData(BaseViewHolder holder, T t);

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addData(List<T> data){
        if(data!=null&&data.size()>0){
            mList.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void refreshData(List<T>data){
        if(data!=null&& data.size()>0){
            mList.clear();
            mList.addAll(data);
            notifyDataSetChanged();
        }
    }


    public class BaseViewHolder extends RecyclerView.ViewHolder {
          SparseArray<View> mMap= new SparseArray();

        public BaseViewHolder(View itemView) {
            super(itemView);
        }
        public View findView(@IdRes int id){

            View view = mMap.get(id);
            if(view==null){
                view=itemView.findViewById(id);
                mMap.put(id,view);
            }
            return view;
        }
        public void setText(@IdRes int id,String msg){
            View view=(TextView)findView(id);
            if(view instanceof TextView){
                TextView view1= (TextView) view;
                view1.setText(msg);
            }
        }
    }
    public interface OnItemClickListener{
        void onItemClick(View v,int position);
    }

    public void setmOnItemTouchListener (OnItemClickListener onItemClickListener){
        mOnItemTouchListener=onItemClickListener;
    }

    protected abstract int getLayout();


}
